package com.sunshine.lnuplus.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.ui.activity.LoadSchoolLessonWebViewActivity
import com.sunshine.lnuplus.ui.activity.SuperLoginActivity
import com.sunshine.lnuplus.utils.TagHelper
import java.util.*
import kotlin.Comparator
import kotlin.collections.ArrayList

/**
 * Created by sunshine on 2020/4/3
 */

class LoadSchoolAdapter(
    private val activity: AppCompatActivity,
    private val schoolList: ArrayList<ArrayList<String>>
) : RecyclerView.Adapter<LoadSchoolAdapter.RecyclerHolder>(){

    init {
        val school = ArrayList<String>().apply {
            add("云南师范大学文理学院")
            add("yunnanshifandaxuewenlixueyuan")
            add("http://jxgl.ysdwl.cn/")
            add("ZF")
        }
        val school1 = ArrayList<String>().apply {
            add("通用正方教务管理")
            add("#")
            add("http://m.baidu.com/")
            add("ZF")
        }
        val school2 = ArrayList<String>().apply {
            add("吉林艺术学院")
            add("jinlinyishuxueyuan")
            add("http://125.223.232.67/")
            add("XQZ")
        }
        val school3 = ArrayList<String>().apply {
            add("安徽大学")
            add("anhuidaxue")
            add("https://jwxt2.ahu.edu.cn/")
            add("ZF")
        }
        val school4 = ArrayList<String>().apply {
            add("中南民族大学")
            add("zhongnanminzudaxue")
            add("http://ehall.scuec.edu.cn/new/index.html")
            add("ZNMZ")
        }
        val school5 = ArrayList<String>().apply {
            add("安徽科技学院")
            add("anhuikejixueyuan")
            add("http://jwxt.ahstu.edu.cn/eams/login.action")
            add("AHKJ")
        }
        val school6 = ArrayList<String>().apply {
            add("南京审计大学金审学院")
            add("nanjingshenjidaxuejinshenxueyuan")
            add("http://222.193.112.78/jwc/Students/default.aspx?r=0.710343064605418&d=59B7BB9FD0B929E105B2FF0726637085")
            add("NJSJJS")
        }
        val school7 = ArrayList<String>().apply {
            add("华中农业大学")
            add("huazhongnongyedaxue")
            add("http://jwgl.hzau.edu.cn/xtgl/login_slogin.html")
            add("XZF")
        }
        val school8 = ArrayList<String>().apply {
            add("通用新正方教务管理")
            add("#")
            add("http://m.baidu.com/")
            add("XZF")
        }
        val school9 = ArrayList<String>().apply {
            add("通用强智教务管理")
            add("#")
            add("http://m.baidu.com/")
            add("QZ")
        }
        val school10 = ArrayList<String>().apply {
            add("四川工程职业技术学院")
            add("sichuangongchengzhiyejishuxueyuan")
            add("http://jiaowu.scetc.edu.cn/")
            add("SCGCZYJS")
        }
        val school11 = ArrayList<String>().apply {
            add("大连工业大学")
            add("dajiangongyedaxue")
            add("http://210.30.62.38:8080/jsxsd")
            add("QZ")
        }
        val school12 = ArrayList<String>().apply {
            add("通用新强智教务管理")
            add("#")
            add("http://m.baidu.com/")
            add("XQZ")
        }
        val school13 = ArrayList<String>().apply {
            add("广东石油化工学院")
            add("guangdongshiyouhuagongxueyuan")
            add("http://210.38.250.43/")
            add("GDSYHG")
        }

        schoolList.add(school)
        schoolList.add(school1)
        schoolList.add(school2)
        schoolList.add(school3)
        schoolList.add(school4)
        schoolList.add(school5)
        schoolList.add(school6)
        schoolList.add(school7)
        schoolList.add(school8)
        schoolList.add(school9)
        schoolList.add(school10)
        schoolList.add(school11)
        schoolList.add(school12)
        schoolList.add(school13)
        Collections.sort(schoolList, SchoolComparable())
    }

    class RecyclerHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val button: Button = itemView.findViewById(R.id.button_choose_classname)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerHolder {
        return RecyclerHolder(LayoutInflater.from(activity).inflate(R.layout.item_classname, parent, false))
    }

    override fun getItemCount(): Int {
        return schoolList.size
    }

    override fun onBindViewHolder(holder: RecyclerHolder, position: Int) {
        if(activity.getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
            holder.button.setTextColor(Color.WHITE)
        }else{
            holder.button.setTextColor(Color.BLACK)
        }
        holder.button.text = schoolList[position][0]
        holder.button.setOnClickListener {
            if(schoolList[position][3] == "QG"){
                activity.startActivityForResult(Intent(activity, SuperLoginActivity::class.java).putExtra("qingguo", schoolList[position][2]), TagHelper.LOADSCHOOLCHOOSE)
            }else{
                val bundle = Bundle()
                bundle.putString("url", schoolList[position][2])
                bundle.putString("type", schoolList[position][3])
                activity.startActivityForResult(Intent(activity, LoadSchoolLessonWebViewActivity::class.java).putExtra("bundle", bundle), TagHelper.LOADSCHOOLCHOOSE)
            }
        }
    }

    fun getIndex(str: String?): Int {
        schoolList.forEach {
            var letter = it[1].substring(0, 1)
            letter = if (letter.matches(Regex("\\w"))) {// 匹配拼音第一个位置是否为a-zA-Z
                letter.toUpperCase()
            } else {
                "#"
            }
            if(letter.replace("#", "☆") == str){
                return schoolList.indexOf(it)
            }
        }
        return -1
    }
}

class SchoolComparable: Comparator<ArrayList<String>>{
    override fun compare(o1: java.util.ArrayList<String>?, o2: java.util.ArrayList<String>?): Int {
        return o1!![1].compareTo(o2!![1])
    }

}